package com.liyuan.help_e.controller;

import com.liyuan.help_e.component.EnumUtils;
import com.liyuan.help_e.model.Comment;
import com.liyuan.help_e.model.Question;
import com.liyuan.help_e.model.ResultJson;
import com.liyuan.help_e.service.CommentService;
import com.liyuan.help_e.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class ShowController {
    @Autowired
    QuestionService questionService;
    @Autowired
    CommentService commentService;
    @GetMapping("/question/{id}")
    public void question(@PathVariable("id")Integer id,
                               HttpServletResponse response,
                               HttpServletRequest request) throws ServletException, IOException {
        request.getRequestDispatcher("/show.html").forward(request,response);
    }
    @PostMapping("/question/{id}")
    public ResultJson question(@PathVariable("id")Integer id){
        Question question = questionService.selectQuestionById(id);
        return new ResultJson(200,"get question success!",question);
    }
    @GetMapping("/recommend/questions/{size}")
    public ResultJson recommendQuestions(@PathVariable("size")Integer size){
        Question[] questions = questionService.selectQuestionsByReadCount(size);
        return new ResultJson(200,"get recommed questions success(由于未集成elasticsearch 故推荐先这样!)",questions);
    }

    @PostMapping("/comment")
    public ResultJson comment(@RequestBody Comment comment){
        commentService.insertComment(comment);
        return new ResultJson(200,"insert comment success",null);
    }

    @GetMapping("/question/{id}/comments")
    public ResultJson getComments(@PathVariable("id")Integer id){
        Comment[] comments = commentService.selectComments(EnumUtils.REPLY_QUESTION.getType(),id);
        return new ResultJson(200,"get comments success!",comments);
    }
    @PostMapping("/praiseComment/{id}")
    public ResultJson praiseComment(@PathVariable("id")Integer commentId){
        commentService.praise(commentId);
        return new ResultJson(200,"praise success!",null);
    }
    @GetMapping("/question/comment/comments/{commentId}")
    public ResultJson getCommentsToComment(@PathVariable("commentId")Integer parentId){
        Comment[]comments = commentService.selectComments(EnumUtils.REPLY_COMMENT.getType(),parentId);
        return new ResultJson(200,"get comments->comment success",comments);
    }

}
